<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html class="win-frame">
<head>
<title>${name_china}管理-新建${name_china}</title>
 <%@include file="/common/head.jsp" %>




</head>
<body class="iframe-page">
<div class="page-title-wrapper">
    <div class="page-title">
        <div class="page-title-text">
            <span>${name_china}管理</span>
            <span class="split"></span>
            <span>新建${name_china}</span>
        </div>
        <a id="goBack" class="btn-return distance-left" href="javascript:;"><i class="icon icon-arrow-left"></i>返回上级</a>
    </div>
    <div class="page-title-border"></div>
</div>

<form id="addForm" method="post">
  <div id="left" style="width:80%;float:left">
	 <div class="form-wrapper">
		<div class="h4 distance-double">
	        <span class="h4-title"><i class="icon icon-setting"></i>基本信息</span>
	        <span class="h4-border"></span>
	    </div>
     
       <!--  <div class="form-cont" id="hiddenvideo">
            <p class="form-cont-title"><span class="field-required">*</span>视频</p>
            这里写表数据的附件字段  fileId
            <input id="fileId" name="fileId" type="text" class="summer-input distance-right">
            <input id="videoId" name="videoId" type="text" class="summer-input distance-right">
            这里一定要写一个id隐藏
            <input type="hidden" id="id" name="id"> 
         </div> -->
     <div class="form-cont">
	        <p class="form-cont-title">视频</p>
	        <input type="hidden" id="vedioUrl" name="vedioUrl"/>
	   </div>
	     
         
	     <div class="form-cont">
	        <p class="form-cont-title">排序号</p>
	        <input type="text" id="reorder" name="reorder" class="summer-input summer-validatebox"  data-validatebox="validType:['noNegativeNumber']"/> 
	     </div>
	     
	     <div class="form-cont">
	        <p class="form-cont-title"><span class="field-required">*</span>视频名称</p>
	        <input type="text" id="fileName" name="fileName" class="summer-input summer-validatebox"  data-validatebox="validType:['required','maxLength[50]']"/> 
	     </div>
	     
	      <div class="form-cont">
	        <p class="form-cont-title">视频类别</p>
	        <input type="text" id="category" name="category" class="summer-input summer-validatebox"  data-validatebox="validType:['maxLength[10]']"/> 
	     </div>
	     
	      <div class="form-cont">
	        <p class="form-cont-title">积分</p>
	        <input type="text" id="bonusPoints" name="bonusPoints" class="summer-input"/> 
	     </div>
	     
<!-- 	     <div class="form-cont">
	        <p class="form-cont-title">作者</p>
	        <input type="text" id="author" name="author" class="summer-input summer-validatebox"  data-validatebox="validType:['maxLength[50]']"/> 
	     </div> -->
	     
	    <!--  <div class="form-cont">
	        <p class="form-cont-title"> 开始时间</p>
	        <input type="text" id="startTime" name="startTime" class=" summer-input summer-validatebox" onFocus="var afterTime=$dp.$('stopTime');WdatePicker({dateFmt:'yyyy-MM-dd',onpicked:function(){afterTime.focus();},minDate:'#F{(\'%y-%M-%d\')}',maxDate:'#F{$dp.$D(\'stopTime\')}'})" /> 
	     </div>
	     
	    <div class="form-cont">
	        <p class="form-cont-title">结束时间</p>
	        <input type="text" id="stopTime" name="stopTime" class="summer-input" onFocus="WdatePicker({dateFmt:'yyyy-MM-dd',minDate:'#F{$dp.$D(\'startTime\')}'})"/> 
	     </div>
	     
	      <div class="form-cont">
	        <p class="form-cont-title">详细时间格式</p>
	        <input type="text" id="dateDetail" name="dateDetail" class="summer-input summer-validatebox"  onFocus="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})" data-validatebox="validType:['maxLength[50]']"/> 
	     </div> -->
	 
<!-- 	    <div class="form-cont">
	        <p class="form-cont-title">出版日期</p>
	        <input type="text" id="publicationDate" name="publicationDate" class="summer-input summer-validatebox"  onFocus="WdatePicker({dateFmt:'yyyy-MM-dd'})" data-validatebox="validType:['maxLength[50]']"/> 
	     </div> -->
	     
<!-- 	     <div class="form-cont">
	        <p class="form-cont-title"><span class="field-required">*</span>级联下拉选项</p>
	        <input type="text" id="tree" name="tree" class="summer-input" style="width: 220px;" /> 
	     </div> -->
	    
	     <div class="form-cont">
	        <p class="form-cont-title">视频简介</p>
	        <textarea class="ckeditor" id="styleText" name="styleText"></textarea>
	     </div>
	     
	      <div class="form-cont">
	        <p class="form-cont-title">备注</p>
	        <textarea class="summer-textarea" id="note" name="note"></textarea>
	     </div>
	      
	
	 <div class="form-btn-wrapper" id="funcbar">
        <a class="btn btn-normal" id="backBtn">关闭</a>
    </div>
  </div>
  </div>
	
</form>
<script>
    var  permission = CommonUtil.getPermission('${name_english}');
    
    $(function () {
    	
    	//初始化下拉选项
    	 $('#category').combobox({
            valueField : 'code',
            textField : 'name',
            url : COMMON_URL.getCode + '?type=T_DM_WEB_VEDIO_CATEGORY'
        });
    	
    	//初始化级联下拉选项
/*     	$("#tree").comboboxTree({
		    url: COMMON_URL.getCode + '?type=T_DM_MODEL_TEMPLET_TREE',
		    cIdKey : "id",
		    pIdKey : "pid",
		    setting: {
		        check: {
		            enable: true,
		            chkStyle: "radio",
		            radioType: "all"
		        }
		    },
		    //panelWidth : 220 ,
		    panelHeight: 200
	   }); */
    	
/*     	$('#price').validatebox({
    			      rules:{
    			            regex: {
    			                validator: function (value) {
    			                    var regex = new RegExp("^\\d+\\.\\d{1,"+3+"}$");
    			                    return regex.test(value);
    			                },
    			                message: '自定义验证浮点数'
    			            }
    			        },
    			        tipPosition: 'right',
    			        validType: ['regex[3]']
    	}); */
    	
    	  //初始化附件
	    //initUploadConfig();
	   
    	//初始化funcbar
     	$('#funcbar').prepend($.funcbar({
            type: 'button',
            data: permission,
            permit: ['${name_english}:add']
        }));
   	 
     	 $("#vedioUrl").filePlugin({
     		   type:"video"
     	   });
    	//保存按钮
    	$('#funcbar').on('click', 'a[data-permit="${name_english}:add"]', {isContinue: false},  submit);
    	
    	//返回按钮
    	$('#backBtn,#goBack').on('click',backMain);
    	
    });
    
    
	function initUploadConfig(){
		
		initMyUploadConfig("fileId","videoId","hiddenvideo","T_MODEL_TEMPLET","${pageContext.session.id}");
		
	}
	
	  //初始化配置信息
	  function initMyUploadConfig (fileId,videoId,hiddenvideo,tableName,sessionId){
         //隐藏视频变量
	        $("#"+fileId).attr({"type" : "hidden"});
	        $("#"+videoId).attr({"type" : "hidden"});
          var vediostr = `<img id="VideoViewId" src="" width="80px" onerror="javascript:this.src='` + CONST_URL.BASEIMAGE +`'"/>`;
         vediostr = vediostr + `<input name="video" type="file" accept="video/mp4" id="video">`;
         $("#"+hiddenvideo).append(vediostr);
          initMutiFile(fileId,videoId,hiddenvideo,tableName,sessionId);
     }

     //初始化图片和视频添加事件(记得动态添加表名)
      function initMutiFile(fileId,videoId,hiddenvideo,tableName,sessionId){
	

        //初始化视频上传事件
        var filtwo=$("#"+hiddenvideo); 
        filtwo.bind('change',function(){  

                    //1 上传视频到文件服务器  返回视频参数  =   视频id  视频地址  视频时长  视频第一帧地址 视频类型 视频大小  视频的属于哪个表
                    //id  fileUrl viewUrl itemType fileType fileSize ctime status dtime
            
                      var formdata = new FormData(); 
                      formdata.append("file", $("#video").get(0).files[0]);
                      formdata.append("ind", 2);
                      formdata.append("userId", sessionId);
                      formdata.append("fileType", "vedio");
                      formdata.append("tableName", tableName);

                      $.ajax({
                          type: "POST",
                          //url: "http://192.168.199.124:8080/FileServer/upload",
                          url: COMMON_URL.upload,
                          dataType: "json",//返回值
                          data: formdata,
                          sync:true,
                          cache : false, 
                          contentType : "multipart/form-data-urlencoded; charset=utf-8", 
                          contentType:false, //必须false才会避开jQuery对 formdata 的默认处理 , XMLHttpRequest会对 formdata 进行正确的处理
                          processData: false, //必须false才会自动加上正确的Content-Type 
                          success: function (ro) {
                          	//var jsonString = JSON.stringify(ro);
                              if (ro.code == 200) {
                                  //图片上传成功回调
                                  //alert(ro.datas.id);
                                  var ro_videoId = uploadFile(ro.datas,"vedioId");
                                  $("#videoId").attr({'value': ro_videoId});
                                  //归集到一个ID中
                                  //无法断定是 图片ID 还是视频ID时  (由于图片ID不禁用 视频ID禁用和清空)可以声明一个字段
                                  //最好的办法是再声明一个imageId 可以实时 fileId = imageId + vedioId;
                                  $("#fileId").attr({'value':ro_videoId});
                                  $("#VideoViewId").attr({'src':ro.datas.url});
                                 // alert("视频数据到位:"+$("#videoId").val());
                                 // alert("文件ID(图片+视频)数据到位:"+$("#fileId").val());
                              }
                              else {//图片未上传成功回调
                              	alert(ro.message);
                              }
                          },
                          error: function (err) {
                              //服务器连接失败报错处理
                              alert("上传异常");
                              //alert(err.responseText);
                          },
                          beforeSend: function () {
                              //图片上传之前执行的操作，当前为进度条显示
                              //alert("图片正在上传中...");
                          }
                      });
            
            //2 返回图片 id 和 图片地址  把图片id放到   fileId 字段中    把src 放到  fileUrl中
            
            //3 这样为最后提交准备就绪  把videoId  和    vedioUrl  viewUrl 保存到数据库中
        });
   }
     
    //上传图片和视频数据 + 文件字段
      function uploadFile(data,fieldv){
       	var ro_fileId = null;
          //保存数据到数据库   返回id
          $.sajax({
      		type : "get",
      		async : false,
      		//url : basePath + '/fileInfo/add',
      		url : COMMON_URL.fileinfoadd,
      		onLoadSuccess : function(ro){
      			if (ro.result == "success"){
      				//alert(ro.data);
      				ro_fileId = ro.data;
      			}
      		},
      		data:{
      			"tableName" : data.tableName,
      			"businessId" : data.id,
      			"field" : fieldv,
      			"fileName" : data.fileName,
      			"fileSuffix" : data.fileSuffix,
      			"fileSize" : data.fileSize,
      			"filePath" : data.url,
      			"vedioPath" : data.vedioUrl,
      			"fileTime" : data.fileLongTime
      		}
      	});
      	return ro_fileId;
      }
    
      //   用 fileIds 获取文件列表 field为列表返名
      function getFileInfos(fileIds,field){
      	 var fileInfo = null;
          $.sajax({
       		type : "get",
       		async : false,
       		//url : basePath + '/fileInfo/getFileInfos',
       		url : COMMON_URL.getFileInfos,
       		onLoadSuccess : function(ro){
       			if (ro.result == "success"){
       				//alert(ro.data);
       				fileInfo = ro.data;
       			}
       		},
       		data:{
       			"fileIds" : fileIds,
       			"field" : field,
       		}
       	});
      	 
          return fileInfo;
      }


    
    
    //表单提交
    function submit(event) {
         $("#addForm").form({
        	 buttons : ['#saveBtn','#saveBtnContinue'],
             ajax: {
                 url: CONST_URL.ADD
             },
             onSubmitSuccess: function (data) {
            	 $.stip({
  	                type:'ok',//显示类型 有info、 ok、 danger
  	                text: '保存成功！',
  	                onBeforeHide: function () {
  	            	  var isContinue = event.data.isContinue;
  	            	  //alert(isContinue);
                   	  if (isContinue) {
                          resetForm();
                   	  } else {
                   		  backMain();
                   	  }
  	               }
              	});
              }
         }).form("submit");
    }
    
    //保存并继续清空form
    function resetForm(){
        //$('#xm,#zw,#cs,#bgdh,#sjhm').val('');
    }
    
    //返回主页面
    function backMain() {
    	window.location.href = CONST_URL.INIT_MAIN;
    }
    
   
    //请求url
    var CONST_URL = {
    	INIT_MAIN: basePath + "${common_url}init",
       	ADD: basePath + "${common_url}add",
       	BASEIMAGE: basePath + "/static/images/addimage.png",
    };
    
</script>
</body>
</html>